<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
    
    protected function _initAutoload() {
        $autoloader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => '',
                    "basePath" => APPLICATION_PATH . "/modules/default",
                ));
        return $autoloader;
    }

      protected function _initActionHelpers() {
        Zend_Controller_Action_HelperBroker::addHelper(new Custom_Action_Helper_AssetsList());
    }


    protected function _initViewHelpers() {
        $this->bootstrap('layout');
        $layout = $this->getResource('layout');
        #Set view properties
        $view = $layout->getView();
        $view->setHelperPath('Custom/View/Helper', 'Custom_View_Helper');
        $view->doctype('XHTML1_STRICT');
        $view->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
        $view->headTitle()->setSeparator(' - ');
        $view->headTitle('Myconnection');

        #error helper override
        $formErrors = $view->getHelper('formErrors');
        $formErrors->setElementStart('<span class="note error">')
                ->setElementSeparator('<br />')
                ->setElementEnd('</span>');
    }

    protected function _initProjectSpecific() {
        #application logger
        $logger = new Zend_Log();
        $writer1 = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/data/logs/info');
        $logger->addWriter($writer1);

        $writer2 = new Zend_Log_Writer_Stream(APPLICATION_PATH . '/data/logs/error');
        $logger->addWriter($writer2);

        // add a filter only to writer2
        $filter = new Zend_Log_Filter_Priority(Zend_Log::INFO, '>=');
        $writer1->addFilter($filter);

        $filter = new Zend_Log_Filter_Priority(Zend_Log::NOTICE);
        $writer2->addFilter($filter);

        Zend_Registry::set('logger', $logger);
        #end of logger


        $this->bootstrap("db");
        $db = $this->getResource("db");

        if ($db->getProfiler()->getEnabled()) {
            $db->setProfiler(new Zend_Db_Profiler_Firebug('All Db Queries'));
            $db->getProfiler()->setEnabled(true);
        }

        $vizzoSession = new Zend_Session_Namespace('myconnection');
        Zend_Registry::set('mySession', $vizzoSession);
        
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/config.ini', APPLICATION_ENV);
        Zend_Registry::set('config', $config);
        
    }

    protected function _initRoutes() {
        $config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini');
        $router = Zend_Controller_Front::getInstance()->getRouter();
        $router->addConfig($config, 'routes');
    }



    protected function _initPlugins() {
        // Access plugin
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new Plugin_ModuleLayoutLoader());
        $front->registerPlugin(new Plugin_SetLeftNav());
    }

    protected function _initNavigation() {
        #set translation in navigation
        $locale = (isset($mySession->language)) ? $mySession->language : 'en';
        $navigation = null;
        if (file_exists(APPLICATION_PATH . '/configs/navigation/' . $locale . 'topnav.xml')) {
            $config = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation/' . $locale . 'topnav.xml', 'nav');
            $navigation = new Zend_Navigation($config);
             Zend_Registry::set('topnav', $navigation);

        }
        if (file_exists(APPLICATION_PATH . '/configs/navigation/' . $locale . 'leftnav.xml')) {
            $leftconfig = new Zend_Config_Xml(APPLICATION_PATH . '/configs/navigation/' . $locale . 'leftnav.xml', 'nav');
            $leftnavigation = new Zend_Navigation($leftconfig);
             Zend_Registry::set('leftnav', $leftnavigation);

        }
        // Set the navigation object to the view

        Zend_Layout::getMvcInstance()->getView()->navigation($navigation);
    }    


    protected function _initTranslations() {
        $translate = new Zend_Translate(
                        array(
                            'adapter' => 'array',
                            'scan' => Zend_Translate::LOCALE_FILENAME,
                            'content' => APPLICATION_PATH . '/languages/en.php',
                            'locale' => 'en'
                ));

        $translate->addTranslation(
                array(
                    'content' => APPLICATION_PATH . '/languages/fr.php',
                    'locale' => 'fr'
        ));


        $mySession = Zend_Registry::get('mySession');

        if (isset($mySession->language) && in_array($mySession->language, $translate->getList())) {
            $lang = $mySession->language;
        } else {
            $lang = 'en';
        }

        $translate->setLocale($lang);

        Zend_Registry::set('translate', $translate);
    }
  

}

